package com.weng.sso.server.api;

import com.weng.framework.core.model.ResponseData;
import com.weng.sso.core.config.SsoConfig;
import com.weng.sso.core.constants.LoginTypeEnum;
import com.weng.sso.core.model.AuthReq;
import com.weng.sso.core.model.SsoUser;
import com.weng.sso.server.helper.SsoTokenLoginHelper;
import com.weng.sso.server.service.AuthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/*
 *  基于token的单点登陆(例如app)
 * @author wengzhonghui
 * @date 10:14 2019/4/23
 */
@Controller
@RequestMapping("/auth")
public class AppController {

    @Autowired
    private AuthService authService;

    /*
     * 登陆
     * @return
     */
    @PostMapping(SsoConfig.SSO_LOGIN)
    @ResponseBody
    public ResponseData<SsoUser> login(@RequestBody AuthReq authReq) {

        return authService.login(authReq.getUserName(), authReq.getPassword(), LoginTypeEnum.LOGIN_TOKEN.getCode());
    }


    /*
     * 注销
     * @param [token]
     * @return
     */
    @PostMapping(SsoConfig.SSO_LOGOUT)
    @ResponseBody
    public ResponseData logout(@RequestBody AuthReq authReq) {
        if(StringUtils.isEmpty(authReq.getToken())){
            return ResponseData.failed("Logout fail! Token is empty!");
        }
        SsoTokenLoginHelper.logout(authReq.getToken());
        return ResponseData.success("Logout Success!");
    }

    /*
     * 获取当前用户基础信息
     * @param [token]
     * @return
     */
    @PostMapping(SsoConfig.SSO_USER_INFO )
    @ResponseBody
    public ResponseData getUserInfo(@RequestBody AuthReq authReq) {
        SsoUser ssoUser = SsoTokenLoginHelper.getUserInfoByToken(authReq.getToken());
        if (ssoUser == null) {
            return  ResponseData.failed("Token is error!");
        }
        return ResponseData.success(ssoUser);
    }

    /*
     * 登录检查
     * @param [token]
     * @return
     */
    @PostMapping(SsoConfig.SSO_LOGIN_CHECK )
    @ResponseBody
    public ResponseData loginCheck(@RequestBody AuthReq authReq) {
        SsoUser ssoUser = SsoTokenLoginHelper.loginCheck(authReq.getToken());
        if (ssoUser == null) {
            return  ResponseData.failed("Not Login!");
        }
        return ResponseData.success(ssoUser);
    }


    /*
     * 修改密码
     * @param [token]
     * @return
     */
    @PostMapping(SsoConfig.SSO_CHANGE_PWD )
    @ResponseBody
    public ResponseData changePwd(@RequestBody AuthReq authReq) {
        SsoUser ssoUser = SsoTokenLoginHelper.loginCheck(authReq.getToken());
        if (ssoUser == null) {
            return  ResponseData.failed("Token is Error!");
        }
        return this.authService.changePwd(ssoUser.getAccount(),authReq.getNewPassword(),authReq.getPassword());
    }
}